package com.retail.msg.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.result.Result;
import com.retail.msg.entity.vo.RetailMsgVo;
import com.retail.core.entity.ParamReq;
import com.retail.msg.entity.request.RetailMsgRequest;
import com.retail.msg.enums.DelayFlagEnums;
import com.retail.msg.enums.MsgTypeEnums;
import com.retail.msg.mapper.RetailMsgMapper;
import com.retail.msg.service.RetailMsgService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息 Service impl
 *
 * @author Administrator
 */
@Service
public class RetailMsgServiceImpl implements RetailMsgService {

    @Resource
    private RetailMsgMapper retailMsgMapper;

    @Override
    public RetailMsgVo getModel(Long id) {
        RetailMsgVo retailMsgVo = retailMsgMapper.getModel(id);
        retailMsgVo.setMsgTypeName(MsgTypeEnums.getStatusValues(retailMsgVo.getMsgType()));
        retailMsgVo.setDelayFlagName(DelayFlagEnums.getStatusValues(retailMsgVo.getDelayFlag()));
        retailMsgVo.setDelayYmdTime(DateUtil.format(retailMsgVo.getDelayTime(), "MM-dd"));
        return retailMsgVo;
    }

    @Transactional
    @Override
    public Long save(RetailMsgRequest retailMsgRequest) {
        retailMsgMapper.save(retailMsgRequest);
        Long id = retailMsgRequest.getId();
        if (ObjectUtil.equal(retailMsgRequest.getDelayFlag(), 1)) {
            delayMsgById(id);
        }
        return id;
    }

    @Transactional
    @Override
    public Long update(RetailMsgRequest retailMsgRequest) {
        retailMsgMapper.update(retailMsgRequest);
        if (ObjectUtil.equal(retailMsgRequest.getDelayFlag(), 1)) {
            delayMsgById(retailMsgRequest.getId());
        }
        return retailMsgRequest.getId();
    }

    @Override
    public List<RetailMsgVo> listPage(ParamReq<RetailMsgRequest> params) {
        params.setStartIndex((params.getCurrent() - 1) * params.getSize());
        List<RetailMsgVo> res = retailMsgMapper.listPage(params);
        res.forEach(re -> {
            re.setMsgTypeName(MsgTypeEnums.getStatusValues(re.getMsgType()));
            re.setDelayFlagName(DelayFlagEnums.getStatusValues(re.getDelayFlag()));
            re.setDelayYmdTime(DateUtil.format(re.getDelayTime(), "MM-dd"));
        });

        return res;
    }

    @Override
    public Integer countPage(ParamReq<RetailMsgRequest> params) {
        return retailMsgMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return retailMsgMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Result<Integer> delayMsg(Long id) {
        Integer delayCount = delayMsgById(id);
        return Result.success(delayCount);
    }

    @Override
    public List<RetailMsgVo> findMsgLikeMsgUser(String msgUser, String msgContent) {
        List<RetailMsgVo> ret =  retailMsgMapper.findMsgLikeMsgUser(msgUser, msgContent);
        ret.forEach(r -> {
            r.setMsgTypeName(MsgTypeEnums.getStatusValues(r.getMsgType()));
            r.setDelayYmdTime(DateUtil.format(r.getDelayTime(), "MM-dd"));
        });
        
        return ret;
    }

    @Override
    public Integer updateState(Long id, String adminId) {
        return retailMsgMapper.updateStatus(id, adminId);
    }

    /**
     * 推送通知
     * @param id
     * @return
     */
    public Integer delayMsgById(Long id) {
        return retailMsgMapper.delayMsgById(id);
    }
}
